<?php
defined('IN_IA') or exit('Access Denied');
//开发模式
define('KUYUAN_DEBUG',false);
KUYUAN_DEBUG ? error_reporting(E_ALL & ~E_NOTICE) : error_reporting(0);


/**
 * 执行类
 * @param $_className
 * @return mixed
 */
function execClass($_className)
{
	return new $_className();
}

/**
 * 执行方法
 * @param $_class
 * @param $_methodName
 * @return
 */
function execMethod($_class,$_methodName)
{
	return $_class->$_methodName();
}

class kuyuan_vbzModuleSite extends WeModuleSite {
	public function doWebApi()
	{
		$this->api();
	}

	public function doMobileApi()
	{
		$this->api();
	}

	private function api()
	{
		require MODULE_ROOT.'/vendor/autoload.php';
		global $_GPC;
		if(KUYUAN_DEBUG){
            $this->ajax();
		    $this->createRequestParamFile();
        }
		if(isset($_GPC['pages']) && !empty($_GPC['pages'])){
			if(!is_file(MODULE_ROOT.'/action/'.ucwords($_GPC['pages']).'.php')){
				exit(ucwords($_GPC['pages']).'.php文件不存在');
			}
			$_action = call_user_func('execClass','App\\'.ucwords($_GPC['pages']));
			if(isset($_GPC['doing']) && !empty($_GPC['doing'])){
				$_methodName = $_GPC['doing'];
			}else{
				$_methodName = 'index';
			}
			if(!method_exists($_action,$_methodName)){
				exit($_methodName.'()方法不存在');
			}
			call_user_func('execMethod',$_action,$_methodName);
		}
	}


	/**
	 * 外部ajaxq请求
	 */
	private function ajax()
	{
		header("Access-Control-Allow-Origin:*");
		header("Access-Control-Allow-Origin: *");
		header('Access-Control-Allow-Headers:Authorization');
		header("Access-Control-Allow-Methods: GET, POST, DELETE");
		header("Access-Control-Allow-Credentials: true");
		header("Access-Control-Allow-Headers: Content-Type, X-Requested-With, Cache-Control,Authorization");
	}

    /**
     * 创建请求参数文件，用户ab抗压测试
     */
	private function createRequestParamFile()
    {
        if(count($_POST) <= 0) return;
        $_content = '';
        foreach($_POST as $_k=>$_v){
            $_content .= $_k.'='.$_v.'&';
        }
        file_put_contents(MODULE_ROOT.'/post.text',$_content);
    }


}
